package com.abc.pro.api;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Pair;

import com.abc.pro.model.EventInfo;
import com.abc.pro.model.EventType.Ah;
import com.abc.pro.model.EventType.Ah1st;
import com.abc.pro.model.EventType.O1x2;
import com.abc.pro.model.EventType.O1x21st;
import com.abc.pro.model.EventType.Ou;
import com.abc.pro.model.EventType.Ou1st;
import com.abc.pro.model.GroupEvents;
import com.abc.pro.model.OddsEarlyEvent;
import com.abc.pro.model.OddsInplayEvent;
import com.abc.pro.model.OddsTodayEvent;
import com.abc.pro.util.Logger;

public class OddsParser {
    public static GroupEvents<OddsTodayEvent> parseTodayJson(JSONObject reponse) throws JSONException {
        GroupEvents<OddsTodayEvent> eventlst = new GroupEvents<OddsTodayEvent>();
        if(reponse == null) return null;
        JSONObject jsToday =  reponse.optJSONObject("n-ot");
        if(jsToday == null) return null;
        JSONArray jsArrEs = jsToday.optJSONArray("egs");
        if(jsArrEs != null){
            int len = jsArrEs.length();
            for (int i = 0; i < len; i++) {
                JSONObject jsEvent =  jsArrEs.getJSONObject(i);
                JSONObject jsTmp = jsEvent.optJSONObject("c");
                long compID = jsTmp.getLong("k");
                String compName = jsTmp.getString("n");
                // parse info
                JSONArray jsEs = jsEvent.optJSONArray("es");
                if(jsEs == null) continue;
                // parse match list info
                int jsEsSize = jsEs.length();
                ArrayList<OddsTodayEvent> oddsTodays = new ArrayList<OddsTodayEvent>();
                for(int j=0; j< jsEsSize; j++){
                    jsTmp = jsEs.optJSONObject(j);
                    OddsTodayEvent event = new OddsTodayEvent(compID, compName);
                    EventInfo info = parseEventInfo(jsTmp);
                    if(info == null) continue;
                    event.setInfo(info);
                    jsTmp = jsTmp.optJSONObject("o");
                    event.set1x2(parseO1x2(jsTmp));
                    event.setAh(parseHDP(jsTmp));
                    event.setOu(parseOU(jsTmp));
                    event.set1x21st(parseO1x21st(jsTmp));
                    event.setAh1st(parseAh1st(jsTmp));
                    event.setOu1st(parseOu1st(jsTmp));

                    oddsTodays.add(event);
                }
                eventlst.addEvent(new Pair<String, ArrayList<OddsTodayEvent>>(compName, oddsTodays));
            }
        }
        return eventlst;
    }
    
    public static GroupEvents<OddsEarlyEvent> parseEarlyJson(JSONObject reponse) throws JSONException {
        GroupEvents<OddsEarlyEvent> eventlst = new GroupEvents<OddsEarlyEvent>();
        if(reponse == null) return null;
        JSONObject jsEarly =  reponse.optJSONObject("n-ot");
        if(jsEarly == null) return null;
        JSONArray jsArrEs = jsEarly.optJSONArray("egs");
        if(jsArrEs != null){
            int len = jsArrEs.length();
            for (int i = 0; i < len; i++) {
                JSONObject jsEvent =  jsArrEs.getJSONObject(i);
                JSONObject jsTmp = jsEvent.optJSONObject("c");
                long compID = jsTmp.getLong("k");
                String compName = jsTmp.getString("n");
                // parse info
                JSONArray jsEs = jsEvent.optJSONArray("es");
                if(jsEs == null) continue;
                // parse match list info
                int jsEsSize = jsEs.length();
                ArrayList<OddsEarlyEvent> oddsEarlys = new ArrayList<OddsEarlyEvent>();
                for(int j=0; j< jsEsSize; j++){
                    jsTmp = jsEs.optJSONObject(j);
                    OddsEarlyEvent event = new OddsEarlyEvent(compID, compName);
                    EventInfo info = parseEventInfo(jsTmp);
                    if(info == null) continue;
                    event.setInfo(info);
                    jsTmp = jsTmp.optJSONObject("o");
                    event.set1x2(parseO1x2(jsTmp));
                    event.setAh(parseHDP(jsTmp));
                    event.setOu(parseOU(jsTmp));
                    event.set1x21st(parseO1x21st(jsTmp));
                    event.setAh1st(parseAh1st(jsTmp));
                    event.setOu1st(parseOu1st(jsTmp));
                    oddsEarlys.add(event);
                }
                eventlst.addEvent(new Pair<String, ArrayList<OddsEarlyEvent>>(compName, oddsEarlys));
            }
        }
        return eventlst;
    }
    
    public static GroupEvents<OddsInplayEvent> parseImplayJson(JSONObject reponse) throws JSONException {
        GroupEvents<OddsInplayEvent> eventlst = new GroupEvents<OddsInplayEvent>();
        if(reponse == null) return null;
        JSONArray jsArrEs =  reponse.optJSONArray("i-ots");
        jsArrEs = jsArrEs.getJSONObject(0).optJSONArray("egs");
        if(jsArrEs != null){
            int len = jsArrEs.length();
            for (int i = 0; i < len; i++) {
                // OddsImplayEvent event = new OddsImplayEvent();
                JSONObject jsEvent =  jsArrEs.getJSONObject(i);
                JSONObject jsTmp = jsEvent.optJSONObject("c");
                long compID = jsTmp.getLong("k");
                String compName = jsTmp.getString("n");
                JSONArray jsEs = jsEvent.optJSONArray("es");
                if(jsEs == null) continue;
                // parse match list info
                int jsEsSize = jsEs.length();
                ArrayList<OddsInplayEvent> oddsInplays = new ArrayList<OddsInplayEvent>();
                for(int j=0; j< jsEsSize; j++){
                    OddsInplayEvent event = new OddsInplayEvent(compID, compName);
                    jsTmp = jsEs.optJSONObject(j);
                    EventInfo info = parseEventInfo(jsTmp);
                    if(info == null) continue;
                    event.setInfo(info);
                    jsTmp = jsTmp.optJSONObject("o");
                    event.set1x2(parseO1x2(jsTmp));
                    event.setAh(parseHDP(jsTmp));
                    event.setOu(parseOU(jsTmp));
                    // add match to list
                    oddsInplays.add(event);
                }
                eventlst.addEvent(new Pair<String, ArrayList<OddsInplayEvent>>(compName, oddsInplays));
            }
        }
        return eventlst;
    }
    
    private static EventInfo parseEventInfo(JSONObject jsObj){
        if(jsObj == null) return null;
        EventInfo info = new EventInfo();
        try {
            info.ground = jsObj.getString("g");
            info.id = jsObj.getLong("k");
            info.egid = jsObj.getInt("egid");
            info.egn = jsObj.getString("egn");
            info.n_o = jsObj.getString("n-o");
            info.p_o = jsObj.getString("p-o");
            JSONArray arrI = jsObj.optJSONArray("i");
            info.iHomeTeam = arrI.getString(0);
            info.iAwayTeam = arrI.getString(1);
            info.iDate = arrI.getString(4);
            info.iTime = arrI.getString(5);
        } catch (JSONException e) {
            Logger.e("parseEventInfo() failed");
        }
        return info;
    }
    
    private static O1x2[] parseO1x2(JSONObject jsObj){
        if(jsObj == null || jsObj.isNull("1x2")) return null;
        JSONArray arrI = jsObj.optJSONArray("1x2");
        int size = arrI.length() / 6;
        O1x2[] infos = new O1x2[size];
        for(int i=0; i< size;i++){
            O1x2 info = new O1x2();
            info.homeWinID = arrI.optString(0);
            info.homeWinvalue = arrI.optString(1);
            info.awayWinID = arrI.optString(2);
            info.awayWinValue = arrI.optString(3);
            info.drawID = arrI.optString(4);
            info.drawValue = arrI.optString(5);
            infos[i] = info;
        }
        return infos;
    }
    
    private static Ah[] parseHDP(JSONObject jsObj){
        if(jsObj == null|| jsObj.isNull("ah")) return null;
        JSONArray arrI = jsObj.optJSONArray("ah");
        int size = arrI.length() / 8;
        Ah[] infos = new Ah[size];
        for(int i=0; i< size;i++){
            Ah info = new Ah();
            info.homeID = arrI.optString(0);
            info.homeValue = arrI.optString(1);
            info.awayID = arrI.optString(2);
            info.awayValue = arrI.optString(3);
            info.homeOddsID = arrI.optString(4);
            info.homeOddsValue = arrI.optString(5);
            info.awayOddsValue = arrI.optString(6);
            info.awayOddsValue = arrI.optString(7);
            infos[i] = info;
        }
        return infos;
    }
    
    private static Ou[] parseOU(JSONObject jsObj){
        if(jsObj == null|| jsObj.isNull("ou")) return null;
        JSONArray arrI = jsObj.optJSONArray("ou");
        int size = arrI.length() / 8;
        Ou[] infos = new Ou[size];
        for(int i=0; i< size;i++){
            Ou info = new Ou();
            info.overID = arrI.optString(0);
            info.overValue = arrI.optString(1);
            info.underID = arrI.optString(2);
            info.underValue = arrI.optString(3);
            info.overOddsID = arrI.optString(4);
            info.overOddsValue = arrI.optString(5);
            info.underOddsValue = arrI.optString(6);
            info.underOddsValue = arrI.optString(7);
            infos[i] = info;
        }
        return infos;
    }
    
    private static O1x21st[] parseO1x21st(JSONObject jsObj){
        if(jsObj == null|| jsObj.isNull("1x21st")) return null;
        
        JSONArray arrI = jsObj.optJSONArray("1x21st");
        int size = arrI.length() / 6;
        O1x21st[] infos = new O1x21st[size];
        for(int i=0; i< size;i++){
            O1x21st info = new O1x21st();
            info.homeWinID = arrI.optString(0);
            info.homeWinvalue = arrI.optString(1);
            info.awayWinID = arrI.optString(2);
            info.awayWinValue = arrI.optString(3);
            info.drawID = arrI.optString(4);
            info.drawValue = arrI.optString(5);
            infos[i] = info;
        }
        return infos;
    }
    
    private static Ah1st[] parseAh1st(JSONObject jsObj){
        if(jsObj == null|| jsObj.isNull("ah1st")) return null;
        JSONArray arrI = jsObj.optJSONArray("ah1st");
        int size = arrI.length() / 8;
        Ah1st[] infos = new Ah1st[size];
        for(int i=0; i< size;i++){
            Ah1st info = new Ah1st();
            info.homeID = arrI.optString(0);
            info.homeValue = arrI.optString(1);
            info.awayID = arrI.optString(2);
            info.awayValue = arrI.optString(3);
            info.homeOddsID = arrI.optString(4);
            info.homeOddsValue = arrI.optString(5);
            info.awayOddsValue = arrI.optString(6);
            info.awayOddsValue = arrI.optString(7);
            infos[i] = info;
        }
        return infos;
    }
    
    private static Ou1st[] parseOu1st(JSONObject jsObj){
        if(jsObj == null|| jsObj.isNull("ou1st")) return null;
        JSONArray arrI = jsObj.optJSONArray("ou1st");
        int size = arrI.length() / 8;
        Ou1st[] infos = new Ou1st[size];
        for(int i=0; i< size;i++){
            Ou1st info = new Ou1st();
            info.overID = arrI.optString(0);
            info.overValue = arrI.optString(1);
            info.underID = arrI.optString(2);
            info.underValue = arrI.optString(3);
            info.overOddsID = arrI.optString(4);
            info.overOddsValue = arrI.optString(5);
            info.underOddsValue = arrI.optString(6);
            info.underOddsValue = arrI.optString(7);
            infos[i] = info;
        }
        return infos;
    }
}
